<template>
  <div>
<!--    <el-menu :default-active="listQuery.checkState" class="el-menu-demo" mode="horizontal" @select="handleSelect">-->
<!--      <el-menu-item index="0">待审核</el-menu-item>-->
<!--      <el-menu-item index="1">已审核</el-menu-item>-->
<!--    </el-menu>-->
    <!--搜索-->
    <el-form :inline="true" :model="listQuery" size="mini" label-width="100px" style="margin-top:40px">
      <el-form-item label="客户名称：">
        <el-autocomplete
          clearable
          class="inline-input"
          value-key="customerName"
          :fetch-suggestions="querySearch"
          v-model="listQuery.customerName"
          placeholder="请输入内容"
          @select="handleSelectCustomerName"
          :trigger-on-focus="false"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="handleSearchList()"
          type="primary">
          查询
        </el-button>
        <el-button
          v-if="listQuery.checkState !=1"
          @click="senhe('more')"
          type="primary">
          批量审核
        </el-button>
      </el-form-item>
    </el-form>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        ref="mytable"
        row-key="cglId"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column type="selection" min-width="60" align="center" reserve-selection></el-table-column>
        <el-table-column
          label="客户名称"
          min-width="200">
          <template slot-scope="scope">
              {{scope.row.customerName}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="产品线"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsLineName}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="负责人"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="接收人"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.transferUserName}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="转移时间"
          min-width="150">
          <template slot-scope="scope">
            {{scope.row.transferTime}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="审核状态"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.status}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          min-width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="senhe('one',scope.row)" >审核</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <el-dialog :close-on-click-modal="false" title="转移审核" :visible.sync="dialogVisible" width="500px">
      <el-form  :model="form" size="mini" label-width="100px" ref="form">
        <el-alert title="审核不通过时备注必填" type="warning" :closable="false" center></el-alert>
        <el-form-item label="备注：" label-width="80px" style="margin-top:20px">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleConfirm(2)" :loading="btnLoading2">不通过</el-button>
          <el-button type="primary" @click="handleConfirm(1)" :loading="btnLoading">通 过</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import {goodsLineTransferList,transferMove} from '@/api/company'
import {getCustomerOption} from '@/api/partner'
import {formatDate} from '@/utils/date';

import store from '@/store'
import { mapGetters } from 'vuex'

const defaultListQuery={
  customerName:'',
  "pageNum" : 1,      //第几页，默认1
  "pageSize" : 10     //每页几条，默认10条
};

const defaultForm={
  remark:''
};

export default {
  name: "index",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      multipleSelection:[],
      dialogVisible:false,
      btnLoading:false,
      form:JSON.parse(JSON.stringify(defaultForm)),
      btnLoading2:false

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  components : {

  },
  created() {

  },
  filters: {

  },
  methods:{
    handleSelect(key){
      this.listQuery.checkState = key;
      this.handleSearchList();
    },
    //客户名称输入匹配
    querySearch(queryString, cb){
      getCustomerOption({keywords:queryString}).then((res)=>{
        cb(res);
      })
    },

    //选择客户名称
    handleSelectCustomerName(item){
      this.listQuery.customerName = item.customerName
    },

    //多选表格项
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    //确认审核
    handleConfirm(type){
      if(type === 2 && this.form.remark === ''){
       this.$message({
          type: 'error',
          message: '请填写备注!'
        });
       return false
      }
        if(type == 2){
          this.btnLoading2 = true
        }else{
          this.btnLoading = true
        }
        let ids=[];
        for(let i=0;i<this.multipleSelection.length;i++){
          ids.push(this.multipleSelection[i].cglId);
        }
        let data = {};
        data.cglIds = ids.join(",");
        data.type = type;
        data.remark = this.form.remark
        transferMove(data).then((res)=>{
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.mytable.clearSelection();
          this.multipleSelection = [];
          this.btnLoading = false
          this.btnLoading2 = false
          this.dialogVisible = false
          this.getList();
        }).catch(()=>{
          this.btnLoading = false
          this.btnLoading2 = false
        })
    },

    senhe(type,item){
      switch (type) {
        case 'more':
          if(this.multipleSelection.length ==0){
            this.$message({
              message: '请选择要审核的记录',
              type: 'warning',
              duration: 1000
            });
          }else{
            this.dialogVisible = true;
            this.form = JSON.parse(JSON.stringify(defaultForm))
          }
          break;
        case 'one':
          this.multipleSelection.push(item);
          this.dialogVisible = true;
          this.form = JSON.parse(JSON.stringify(defaultForm))
      }

    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },
    //获取列表
    getList() {
      this.listLoading = true;
      goodsLineTransferList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },

  },
}
</script>

<style  lang="scss">

</style>
